package com.chery.media.library.extend

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.Utils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chery.media.library.R

/**
 * @author MoYe_PanDeng
 * @ClassName:
 * @Description: adapter拓展的
 * @date: 4/7/21
 */

/**s
 * 给adapter拓展的，防止重复点击item
 */
var adapterLastClickTime = 0L

fun BaseQuickAdapter<*, *>.setNbOnItemClickListener(interval: Long = 1000, action: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Unit) {
    setOnItemClickListener { adapter, view, position ->
         val currentTime = System.currentTimeMillis()
         if (adapterLastClickTime != 0L && (currentTime - adapterLastClickTime < interval)) {
             return@setOnItemClickListener
         }
        adapterLastClickTime = currentTime
        action(adapter,view,position)
    }
}

/**
 * 给adapter拓展的，防止重复点击item
 */
var adapterChildLastClickTime = 0L
fun BaseQuickAdapter<*, *>.setNbOnItemChildClickListener(interval: Long = 1000,action: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Unit) {
    setOnItemChildClickListener { adapter, view, position ->
        val currentTime = System.currentTimeMillis()
        if (adapterChildLastClickTime != 0L && (currentTime - adapterChildLastClickTime < interval)) {
            return@setOnItemChildClickListener
        }
        adapterChildLastClickTime = currentTime
        action(adapter,view,position)
    }
}

fun BaseQuickAdapter<*, *>.setEmptyListView(imgResId: Int = 0, content: String = "暂无数据", listener: View.OnClickListener? = null){
    View.inflate(Utils.getApp(), R.layout.custom_empty_list, null).apply {
        findViewById<TextView>(R.id.defaultTip).text = content
        if (imgResId == 0) {
            findViewById<ImageView>(R.id.defaultImg).visibility = View.GONE
        } else {
            findViewById<ImageView>(R.id.defaultImg).visibility = View.VISIBLE
            findViewById<ImageView>(R.id.defaultImg).setImageResource(imgResId)
        }
        setOnClickListener(listener)
        //数据得清空才会显示空布局
        this@setEmptyListView.data.clear()
        this@setEmptyListView.setEmptyView(this)
        this@setEmptyListView.notifyDataSetChanged()
    }
}